<template>
  <div class="goodsindex">
    <!-- 搜索条件 -->
    <el-row :gutter="20" class="goodsindex-queryInfo">
      <!-- 商品名称搜索 -->
      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
        <el-input
          class="goodsindex-queryInfo-li"
          v-model="queryInfo.employeeName"
          clearable
          size="small"
          placeholder="请输入员工名称"
        ></el-input>
      </el-col>
      <!-- 商品分类搜索 -->
      <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
        <el-button
          type="primary"
          class="goodsindex-queryInfo-li"
          size="small"
          @click="initData"
          >搜索</el-button
        >
      </el-col>
    </el-row>
    <!-- 检索结果 -->
    <el-row :gutter="20" class="goodsindex-list">
      <el-col :span="24">
        <el-table
          :data="tableData.records"
          border
          v-loading="loading"
          size="small"
          style="width: 100%"
        >
          <el-table-column type="index" label="序" width="50">
          </el-table-column>
          <el-table-column prop="employeeName" label="员工姓名" width="180">
          </el-table-column>
          <el-table-column prop="description" label="描述" width="150">
          </el-table-column>
          <el-table-column prop="employeePhone" label="员工电话" width="150">
          </el-table-column>
          <el-table-column
            :formatter="dateFormat"
            prop="createTime"
            label="创建时间"
            width="150"
          >
          </el-table-column>

          <el-table-column label="操作" width="350">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="to(`/employees/update/${scope.row.id}`)"
                >查看</el-button
              >
              <el-button
                size="mini"
                type="warning"
                @click="to(`/employees/update/${scope.row.id}`)"
                >修改</el-button
              >

              <el-button size="mini" type="danger" @click="selected = scope.row"
                >删除</el-button
              >

              <el-button
                size="mini"
                type="default"
                @click="checkAccount(scope.row)"
                >查看账号密码</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页 -->
    <el-row :gutter="20" class="goodsindex-list">
      <el-col :span="24" class="goodsindex-page-box">
        <el-pagination
          :hide-on-single-page="true"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <!-- delect -->
    <el-dialog title="提示" :visible.sync="selected" width="30%">
      <span>确定删除{{ selected ? selected.employeeName : "" }}吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="selected = null">取 消</el-button>
        <el-button
          type="primary"
          @click="deleteUser(selected ? selected.id : null)"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="account" width="30%">
      <el-row>
        <span>账号：</span>
        <span>{{ account ? account.account : "" }}</span>
      </el-row>
      <el-row>
        <span>密码：</span>
        <span>{{ account ? account.noMd5 : "" }}</span>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="account = null">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      dialogVisible: false,
      select: "",
      queryInfo: {
        goodsName: "",
        page: 1,
        pageSize: 10,
      },
      selected: null,
      options: [],
      loading: true,
      tableData: [],
      account: null,
    };
  },
  watch: {
    selected(val) {
      console.log(val);
    },
  },
  async created() {
    this.initData();
  },
  methods: {
    async initData() {
      let list = await this.http.post("/techManage/queryPage", {
        pageSize: this.queryInfo.pageSize,
        pageNo: this.queryInfo.page,
        query: {
          ...this.queryInfo,
        },
      });
      this.tableData = list.result;
      this.loading = false;
    },
    handleSizeChange(size) {
      this.queryInfo.pageSize = size;
      this.initData();
    },
    handleCurrentChange(currentPage) {
      this.queryInfo.pageNo = currentPage;
      this.initData();
    },
    to(url) {
      this.$router.push({ path: url });
    },
    async deleteUser(id) {
      if (!id) {
        this.eui.Message({ message: "请选择用户" });
        return false;
      }
      let type, msg;
      let res = await this.http.delete("/techManage/delete?id=" + id);
      if (!res.error) (type = "success"), (msg = "删除成功");
      else (type = "error"), (msg = "删除失败");
      this.eui.Message({
        message: msg,
        type: type,
      });
      this.selected = null;
      this.initData();
    },
    stateFormat(row, column) {
      if (row.status === 0) return "禁用";
      else return "正常";
    },
    async checkAccount(row) {
      let res = await this.http.get("/techManage/getPwd?id=" + row.id);
      this.account = res.error ? null : res.result;
      if (!this.account) {
        this.eui.Message({
          message: `获取员工‘${row.employeeName}’账号信息失败`,
          type: "error",
        });
      }
    },
    dateFormat(row, column) {
      let date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>

<style scoped>
.goodsindex {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
/* 搜索 */
.goodsindex-queryInfo {
  margin-bottom: 10px;
}
.goodsindex-queryInfo-li {
  width: 100%;
  height: auto;
}
/* 列表 */
.goodsindex-list {
  width: 100%;
  height: auto;
  margin-bottom: 20px;
}
/* 分页 */
.goodsindex-page-box {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: flex-end;
}
</style>